package la.renzhen.rtpt.spring;

import la.renzhen.rtpt.EnableConfigurationRegistrar;
import la.renzhen.rtpt.spring.beans.ConfigurationScanner;
import lombok.Setter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;

import java.util.Map;

/**
 * <p>
 *
 * @author <a href="mailto:zhouhaichao@2008.sina.com">haiker</a>
 * @version 09/05/2018 3:52 PM
 */
@Configuration
public class ConfigurationRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware, ResourceLoaderAware {

    @Setter
    Environment environment;

    @Setter
    ResourceLoader resourceLoader;

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        ConfigurationScanner scanner = new ConfigurationScanner(registry);
        scanner.setEnvironment(environment);
        scanner.setResourceLoader(resourceLoader);
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableConfigurationRegistrar.class.getName()));
        String[] scanPackages = annoAttrs.getStringArray("value");
        if(scanPackages.length == 0){
            scanPackages = getComponentScanValue(importingClassMetadata);
        }
        if(scanPackages.length == 0){
            scanPackages = getDefaultScanPackage(importingClassMetadata);
        }
        scanner.scan(scanPackages);

        /*registry.registerBeanDefinition("rtptConfigScanner",
                BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition());*/
    }

    /**
     * 使用{@link org.springframework.context.annotation.ComponentScan} 注解使用的搜索路径
     * @return 搜索路径
     */
    private String[] getComponentScanValue(AnnotationMetadata importingClassMetadata){
        Map<String, Object> annotationAttrs = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
        if(annotationAttrs != null){
            AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationAttrs);
            String[] scanPackages = annoAttrs.getStringArray("basePackages");
            if(scanPackages.length == 0){
                scanPackages = annoAttrs.getStringArray("value");
            }
            return scanPackages;
        }
        return new String[]{};
    }

    /**
     * 搜索默认路径，也就是从注解类的package开始
     * @return 搜索路径
     */
    private String[] getDefaultScanPackage(AnnotationMetadata importingClassMetadata){
        String className = importingClassMetadata.getClassName();
        return new String[]{ClassUtils.getPackageName(className)};
    }
}
